/**
 * 
 */
package com.base.myproject.client.busi.storeslist;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.system.SystemMessage;
import com.base.myproject.client.tools.SendServlet;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;

/**
 * 备货通知，要求供应商备货，供应商通过已经登记的号登录上，自行查询要求供应货物情况
 * @author zgw
 *2007-7-3 下午02:43:04
 * javazgw@gmail.com
 */
public class StoreCcode extends BaseJDBCPanel {
	 BaseStore<BusiObject> store = null;
	 BaseGrid<BusiObject> grid = null;
	//业务代码
	final static String busi =SystemMessage.busi[7];
	public StoreCcode() {
		super();
		init();
	}
/**
 * 为了插入记录建立的构造函数，参数暂时没用
 * 直接调用 insertItem（）生成新纪录
 * @param newitem
 */
	public StoreCcode(String newitem)
	{
		
	}
	public void init() {
		setHeight("400px");
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);  
	    centerData.setMargins(new Margins(5, 5, 0, 0));  
	    this.add(initWeiget(),centerData);
		
		
		registerMainStore(store);
		registerStore(store);

		this.setBusino(busi);
		setdefault();
	}
	private void setdefault()
	{
		store.addDefault("busicode", busi);

	}

	public ContentPanel initWeiget() {
		ContentPanel panel = new ContentPanel();
		panel.setHeading("备货请求");
		panel.setLayout(new BorderLayout());
		
		BorderLayoutData centerData = new BorderLayoutData(LayoutRegion.CENTER);
		centerData.setMargins(new Margins(5, 0, 5, 0));


		
		panel.add(CreateGCodePanel(), centerData);
		
		
		return panel;
	}

	/**
	 * 商品
	 * @return
	 */
	private BaseGrid<BusiObject> CreateGCodePanel() {

		CheckBoxSelectionModel<BusiObject> cbs = new CheckBoxSelectionModel<BusiObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig cgcode = new ColumnConfig("cgicode", "公司货号", 100);
		TextField<String> cgcodetext = new TextField<String>();
		cgcode.setEditor(new CellEditor(cgcodetext));
		columns.add(cgcode);
		
		
		ColumnConfig qty = new ColumnConfig("qty", "数量", 100);
		TextField<String> qtytext = new TextField<String>();
		qty.setEditor(new CellEditor(qtytext));
		columns.add(qty);
		ColumnConfig qtyunit = new ColumnConfig("qtyunit", "单位", 100);
		TextField<String> qtyunittext = new TextField<String>();
		qtyunit.setEditor(new CellEditor(qtyunittext));
		columns.add(qtyunit);
		ColumnConfig upric = new ColumnConfig("upric", "单价", 100);
		TextField<String> uprictext = new TextField<String>();
		upric.setEditor(new CellEditor(uprictext));
		columns.add(upric);


		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		type.addField("icode");
		type.addField("codename");
		type.addField("cgicode");
		type.addField("cgcode");


		type.addField("gcode");
		type.addField("qty");

		type.addField("qtyunit");
		type.addField("upric");


		StoreInfo info = new StoreInfo();

		info.setTablename("storeslistccodeg");
		info.setSql("select * from storeslistccodeg");
		info.setParent(store);// 关联主表
		
	//	info.setLinkparentcolumns(new String[] { "codename" }); // 关联主表字段
		info.setKeyname(new String[] { "icode"}); // 主键
		
		store = new BaseStore<BusiObject>(type, info);
	//	contractgcode.setName("storeslistccodeg");
		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid<BusiObject>(store, cm);

		cbs.setSelectionMode(SelectionMode.SINGLE);

		grid.setSelectionModel(cbs);
		grid.setAutoHeight(true);
		grid.setAutoWidth(true);

		
		registerMainStore(store);
		registerStore(store);
		setMaincomponent(grid);
		return grid;
	}
	public  void insertItem(List<BusiObject> mainModelData,List<BusiObject> ChileModelData)
	{
		SendServlet ss = new SendServlet();
		String codename = ss.getStringx("/"+GWT.getModuleName()+"/BaseServlet", "Action=makecodename&table=storeslistccode&column=codename&beginstr=test");
		
		
		java.util.Vector<String > v = new java.util.Vector<String>();
		Date daynow = new Date();
		DateTimeFormat fmt = DateTimeFormat.getFormat("MM/dd/yyyy");
		String date = fmt.format(daynow);
		
		for(BusiObject md:mainModelData)
		{
			//String codename ="12323";
			String busicode = busi;
		
			String bcode =Env.getEnvbcode();
			String ccode =md.get("ccode");
			String vprepare=Env.getEnvbcode();
			String busistatus =md.get("busistatus");
			String modifydate = date;
			String lastshipdate=md.get("lastshipdate");;
			String sql = "insert storeslistccode (codename,busicode,bcode,ccode,vprepare,busistatus,modifydate,lastshipdate) values('"+codename+"','"+busicode+"','"+bcode+"','"+ccode+"','"+vprepare+"','"+busistatus+"','"+modifydate+"','"+lastshipdate+"')";
			v.add(sql);
		}
		
		for(BusiObject cd:ChileModelData)
		{
			//String codename ="12323";
			String busicode = busi;
		
			String bcode =Env.getEnvbcode();
			String ccode = cd.get("ccode");
			String cgicode=cd.get("cgicode");;
			String gcode=cd.get("gcode");;
			//String scode=cd.get("scode");;
			String qty=cd.get("qty");;
			String qtyunit=cd.get("qtyunit");;
			String upicode=cd.get("upicode");;
			String lastshipdate=cd.get("lastshipdate");
			String busistatus = cd.get("busistatus");
			String sql = "insert storeslistccodeg (codename,busicode,bcode,ccode,cgicode,gcode,qty,qtyunit,upicode,lastshipdate,busistatus) values('"+codename+"','"+busicode+"','"+bcode+"','"+ccode+"','"+cgicode+"','"+gcode+"','"+qty+"','"+qtyunit+"','"+upicode+"','"+lastshipdate+"','"+busistatus+"')";
			v.add(sql);
		}
		
		
		ss.ExcJDBCSQL(v);
	}
}